#! /usr/bin/python
# -*- coding: cp1250 -*-
#sys.path.append(r'C:\x\y')
# obsoleted by csv-pgtb

import glob, sys, os, re, string
import pepe.txt.bigtxt as bt

def columns_spans(inf_mask_s, delimiter_s, skipHeader_b=0):
    """ check and print out csv columns spans """
    out_fl = open('csv-header.out','w')
    out_h = {}

    for inf_s in glob.glob(inf_mask_s):
        out_h = {}
        header_l = []
        flcnt_n = 0
        for line_s in bt.lines(inf_s):
            flcnt_n += 1
            columns_l = string.split(line_s[:-1],'|')
            for index, value in enumerate(columns_l):
                index = 'c'+string.zfill(index+1,3)
                if  skipHeader_b == 1 and flcnt_n == 1:
                    header_l = columns_l
                else:
                    if out_h.get(index):
                        if  out_h[index] < len(str(value)):
                            out_h[index] = len(str(value))
                    else:
                        out_h[index] = len(str(value))
        print out_h
        for i in sorted(out_h.keys()):
            out_fl.write( str(i) +' as '+ header_l[int(i[1:])-1] + '\n' )
        out_fl.write('\n\n')
    out_fl.close()
    return 1

HELP = """Usage: csv-header FILE_S DELIMITER_S SKIPHEADER_B
Purpose: Print text files header.
Example: csv-header test.csv \"|\" 1"""

def main():
    print columns_spans("test.csv", "|", 0)
    """
    if len(sys.argv) == 3:
        columns_spans(sys.argv[1], sys.argv[2])
    elif len(sys.argv) == 4:
        columns_spans(sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        print HELP
    """

if __name__ == "__main__":
    main()
